from rest_framework.views import APIView
import random
from django.shortcuts import render
from django_redis import get_redis_connection
from rest_framework.response import Response
from celery_tasks.sms.tasks import send_sms_code
from . import constants


# 短信验证码
class SMSCodeView(APIView):

    def get(self, request, mobile):
        redis_conn = get_redis_connection('verify_codes')
        send_flag = redis_conn.get("send_flag_%s" % mobile)
        if send_flag:
            return Response({'success': False, 'message': '请求过于频繁'}, status=400)
        # 生成短信验证码
        sms_code = '%06d' % random.randint(0, 999999)
        # redis管道
        pl = redis_conn.pipeline()
        pl.setex("sms_%" % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)
        pl.setex("send_flag_%s" % mobile, constants.SEND_SMS_CODE_INTERVAL, 1)
        # redis执行命令
        pl.execute()
        print("短信验证码:%s" % sms_code)
        # 发送短信验证码
        expires = constants.SMS_CODE_REDIS_EXPIRES // 60
        send_sms_code.delay(mobile, sms_code, expires, constants.SMS_CODE_TEMP_ID)

        return Response({'success': True, 'message': 'ok', 'sms_code': sms_code})
